package com.gxa.controller;

import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.pojo.Interbulletin;
import com.gxa.service.InterBulletinService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@Api(value = "内部公告数据",tags = "内部公告数据接口API-邹明伟")
@RequestMapping("/Int_Bulletin")
public class InterBulletinController {
    @Autowired
    private InterBulletinService interBulletinService;


//    查询
    @RequestMapping(value = "/queryIntBullList",method = RequestMethod.GET)
//    @ApiOperation(value = "查询内部公告数据接口",notes = "传递当前页码数和每页显示记录数以及总条数",httpMethod = "GET", response = JsonResult.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "currentPage",value = "当前页码数",required = true,dataType = "String",paramType = "query"),
//            @ApiImplicitParam(name = "pageSize",value = "每页显示条数",required = true,dataType = "String",paramType = "query")
//    })
//    @ApiResponses({
//            @ApiResponse(code=200,message="请求成功"),
//            @ApiResponse(code=400,message="坏请求"),
//            @ApiResponse(code=404,message="找不到页面"),
//            @ApiResponse(code=405,message="Method请求错误"),
//            @ApiResponse(code=500,message="后台服务错误")
//    })
    @ResponseBody
    public JsonResult queryIntBullList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize){
        return interBulletinService.queryIntBullList(currentPage,pageSize);
    }

    //添加内部公告接口
    @RequestMapping(value = "/addInterbulletin",method = RequestMethod.POST)
    @ResponseBody
    public R addInterbulletin(Interbulletin interbulletin,HttpServletRequest request){
        R r=new R();
        if(interbulletin.getAppendix().isEmpty()){
            r.setCode(500);
            r.setMsg("没有选中文件");
        }else{
            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if(!file.exists()){
                file.mkdirs();
            }
            MultipartFile multipartFile = interbulletin.getAppendix();
            String fileName = multipartFile.getOriginalFilename();
            File newFile = new File(uploadPath + fileName);
            System.out.println("newFile=" + newFile);
            try {
                //上传
                FileCopyUtils.copy(multipartFile.getBytes(),newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件名："+fileName);
            interbulletin.setAppendix_name(fileName);
        }
        return interBulletinService.addIntBu(interbulletin);
    }

    //删除内部公告接口
    @RequestMapping(value = "/delIterB",method = RequestMethod.POST)
    @ResponseBody
    public R deleteInterB( Integer id){
        return interBulletinService.delIterB(id);

    }

//   内部公告搜索
@RequestMapping(value = "/selectInterB",method = RequestMethod.POST)
@ResponseBody
    public JsonResult selectInterB(@RequestBody PageRecv<Interbulletin> pageRecv){
       return interBulletinService.selectInterB(pageRecv);
    }

//    内部公告阅读回复
    @RequestMapping(value = "/replyInterB",method = RequestMethod.POST)

@ResponseBody
    public R replyInterB(@RequestBody Interbulletin interbulletin){
        return interBulletinService.replyInterB(interbulletin);
  }

//  修改公告
    @RequestMapping(value = "/updateInterB",method = RequestMethod.POST)

@ResponseBody
    public R updateInterB(@RequestBody Interbulletin interbulletin){
    return interBulletinService.updateInterB(interbulletin);
    }
}
